package com.qf.android.huihui.view.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.util.Pair;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qf.android.huihui.BR;
import com.qf.android.huihui.R;
import com.qf.android.huihui.adapter.domestic.CommentRecyclerAdapter;
import com.qf.android.huihui.bean.domestic.DataEntity;
import com.qf.android.huihui.bean.domestic.EveryBuyEntity;
import com.qf.android.huihui.bean.domestic.HomeEntity;
import com.qf.android.huihui.bean.domestic.HomeEntity1;
import com.qf.android.huihui.databinding.FragmentDomesticBinding;
import com.qf.android.huihui.utils.retrofit.RHttpShow;
import com.qf.android.huihui.view.fragment.impl.BaseFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;


public class DomesticFragment extends BaseFragment implements Callback<HomeEntity> {
    private FragmentDomesticBinding binding;
    private CommentRecyclerAdapter adapter;
    private RHttpShow service;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_domestic, container, false).getRoot();
        initView(view);
        return view;
    }

    private void initView(View view) {
        binding = DataBindingUtil.bind(view);
        Map<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(DataEntity.class, Pair.create(R.layout.domestic_pager, BR.images));
        map.put(HomeEntity.class, Pair.create(R.layout.domestic_images2, BR.botmivs));
       // map.put(DataEntity.BottomBean.class, Pair.create(R.layout.domestic_images22, BR.botmivs2));
        map.put(EveryBuyEntity.class, Pair.create(R.layout.domestic_allbuy_images3, BR.images3));
        map.put(EveryBuyEntity.ListBeanX.HotSearchBean.class,Pair.create(R.layout.domestic_hotsearch,BR.hotimages));
        adapter = new CommentRecyclerAdapter<>(getContext(), new ArrayList<>(), map);
        binding.domesticRecycler.setAdapter(adapter);

        initData();
    }

    private void initData() {
        service = new Retrofit.Builder()
                .baseUrl("http://baidu.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RHttpShow.class);
        service.getDemestic().enqueue(this);
       service.getEveryBuy().enqueue(new Callback<HomeEntity1>() {
           @Override
           public void onResponse(Response<HomeEntity1> response, Retrofit retrofit) {
               HomeEntity1 body = response.body();
               adapter.add(2,body.getData());

               Log.d("info","---11----"+body.getData().getList().size());
               for (int i = 0; i < body.getData().getList().size(); i++) {
                   if(body.getData().getList().get(i).getHotSearch()!=null && body.getData().getList().get(i).getHotSearch().getClass()== EveryBuyEntity.ListBeanX.HotSearchBean.class){
                       adapter.add(3,body.getData().getList().get(i).getHotSearch());
                       Log.d("info","----i的位置------"+i);
                   }
               }
        //       Log.d("info","---33----"+body.getData().getList().get(0).getHotSearch().getList().size());
           }

           @Override
           public void onFailure(Throwable t) {

           }
       });
    }


    @Override
    public void onResponse(Response<HomeEntity> response, Retrofit retrofit) {
        HomeEntity body = response.body();
        DataEntity data = body.getData();
        Log.d("1620", data.toString());
        adapter.add(0, data);
        adapter.add(1, body);
       // adapter.add(4,body.getData());

    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), "" + t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
